package com.itheima.publisher.config;

import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class MqConfig {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //队rabbitTemplate设置一个return异常处理
    @PostConstruct
    public void initRabbitTemplate(){
        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
            @Override
            public void returnedMessage(ReturnedMessage returnedMessage) {
                System.out.println("-------ReturnsCallback 一般情况下：路由出了问题才会回调！-------");
                System.out.println("交换机： " + returnedMessage.getExchange());
                System.out.println("路由key： " + returnedMessage.getRoutingKey());
                System.out.println("消息： " + returnedMessage.getMessage());
                System.out.println("replyCode： " + returnedMessage.getReplyCode());
                System.out.println("replyText： " + returnedMessage.getReplyText());
                System.out.println("-------ReturnsCallback end-------");
            }
        });
    }
}
